#include "CoordinateTransformer.h"

namespace qtangram {
    QPoint CoordinateTransformer::worldToScreen(const ContainerWidget* widget, const QPointF& point) {
	int size = ((widget->height() < widget->width()) ? widget->height() : widget->width());
    double c = (double)size/widget->getViewRange();
	return QPoint((widget->width()/2)+(int)(point.x()*c),(widget->height()/2)-(int)(point.y()*c) );
    }

    QPointF CoordinateTransformer::screenToWorld(const ContainerWidget* widget, const QPoint& point) {
	int size = ((widget->height() < widget->width()) ? widget->height() : widget->width());
    double c = (double)size/widget->getViewRange();
	return QPointF( ((double)point.x()-(double)widget->width()/2.0)/c   , -((double)point.y()-(double)widget->height()/2.0)/c );
    }

    QPolygon CoordinateTransformer::worldToScreen(const ContainerWidget* widget, const QPolygonF& polygon) {
	QPolygon returned;
	QPolygonF::const_iterator it;
	for(it = polygon.begin(); it != polygon.end(); ++it)
	    returned << worldToScreen(widget, *it);
	return returned;
    }

    QPolygonF CoordinateTransformer::screenToWorld(const ContainerWidget* widget, const QPolygon& polygon) {
	QPolygonF world;
	QPolygon::const_iterator it;
	for(it = polygon.begin(); it != polygon.end(); ++it)
	    world << screenToWorld(widget, *it);
	return world;
    }
}
